import axios, { AxiosHeaders } from 'axios'
import { ElNotification, ElMessage } from "element-plus";

import WebAPI from './api.js'

// 创建axios实例
const service = axios.create({
    baseURL: WebAPI.baseUrl, // api的base_url
    timeout: 5000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
    // Do something before request is sent
    if (config.method === 'post') {
        config.data
    }
    return config
}, error => {
    ElNotification({
        message: error,
        type: "error",
      });
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
    response => {
        return response
    },
    error => {
        console.log('err' + error) // for debug
        return Promise.reject(error)
    }
)

let webRequest = {
    people: {
        getAllPeoleInfoMethod: function (params) {
            return service({
                url: WebAPI.peoPle.getAllPeoleInfo,
                method: 'get',
                params
            })
        },
        deletePeopleByIdMethod: function (params) {
            return service({
                url: WebAPI.peoPle.deletePeopleByid,
                method: 'get',
                params
            })
        },
        createPeopleMethod: function (data) {
            console.log(data);
            return service({
                url: WebAPI.peoPle.createPeople,
                method: 'post',
                data
            })
        },
        editPeopleMethod: function (data) {
            return service({
                url: WebAPI.peoPle.editPeople,
                method: 'post',
                data
            })
        },
    },
    projectDetail: {
        getProjectMethod: function (params) {
            console.log(params, 'params', WebAPI.projectDetail.getProject, WebAPI.baseUrl);
            return service({
                url: WebAPI.projectDetail.getProject,
                method: 'get',
                params
            })
        },
        getProjectByPageMethod: function (params) {
            return service({
                url: WebAPI.projectDetail.getProjectByPage,
                method: 'get',
                params
            })
        },
        deleteProjectMethod: function (data) {
            console.log(data, 'data');
            return service({
                url: WebAPI.projectDetail.deleteProject,
                method: 'post',
                data
            })
        },
        createProjectMethod: function (data) {
            console.log(data, 'data');
             return service({
                url: WebAPI.projectDetail.createProject,
                method: 'post',
                data

            })
        },
        editProjectMethod: function (data) {
            return service({
                url: WebAPI.projectDetail.editProject,
                method: 'post',
                data
            })
        },
        getProjectAllMethod: function (params) {
            return service({
                url: WebAPI.projectDetail.getProjectAll,
                method: 'get',
                params
            })
        },
        getProjectByDate: function (params) {
            return service({
                url: WebAPI.projectDetail.getProjectByDate,
                method: 'get',
                params: {
                    date: params
                }
            })
        },
        deleteProjectBatch: function (data) {
            return service({
                url: WebAPI.projectDetail.deleteProjectBatch,
                method: 'post',
                data
            })
        },
        importProject: function (data) {
            // 上传文件
            return service({
                url: WebAPI.projectDetail.importProject,
                method: 'post',
                data
            })
        },
        exportProject: function (params) {
            // 导出文件
            return service({
                url: WebAPI.projectDetail.exportProject,
                method: 'get',
                params,
                headers: {
                    'Content-Type': 'application/json',
                    'responseType': 'blob'
                }
            })
        }
    }


}


export default webRequest